package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.ShopCarListActivity;
import com.github.postsanf.yinian.activity.TimeGoodsJointActivity;
import com.github.postsanf.yinian.activity.TimeGoodsLomoPreViewActivity;
import com.github.postsanf.yinian.bean.YNShopCar;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.utils.BigDecimalUtil;
import com.github.postsanf.yinian.utils.ImageOptHelper;

import java.util.List;

public class CollectorListAdapter3 extends BaseAdapter {
    private List<YNShopCar> listItems;
    private LayoutInflater layoutinflater;
    private Context context;
    private int itemNum;

    static class ViewHolder {
        private ImageView sec;
        private ImageView image;
        private TextView name;
        private TextView enname;
        private ImageView cut;
        private TextView num;
        private ImageView add;
        private TextView price;
        private TextView shopcar_item_bline;
    }

    public CollectorListAdapter3(Context context, List<YNShopCar> dataSet) {
        this.listItems = dataSet;
        this.context = context;
        this.layoutinflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return listItems.size();
    }

    @Override
    public Object getItem(int position) {
        return listItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final YNShopCar ynShopCar = listItems.get(position);
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = layoutinflater.inflate(R.layout.collectorlist_listitem, null);
            holder.sec = (ImageView) convertView.findViewById(R.id.shopcar_item_sec);
            holder.image = (ImageView) convertView.findViewById(R.id.shopcar_item_image);
            holder.name = (TextView) convertView.findViewById(R.id.shopcar_item_name);
            holder.enname = (TextView) convertView.findViewById(R.id.shopcar_item_enname);
            holder.cut = (ImageView) convertView.findViewById(R.id.shopcar_item_cut);
            holder.num = (TextView) convertView.findViewById(R.id.shopcar_item_num);
            holder.add = (ImageView) convertView.findViewById(R.id.shopcar_item_add);
            holder.price = (TextView) convertView.findViewById(R.id.shopcar_item_price);
            holder.shopcar_item_bline = (TextView) convertView.findViewById(R.id.shopcar_item_bline);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ImageOptHelper.showFrescoStatus(ynShopCar.getEbGoodsPic(), holder.image, context);
        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context, TimeGoodsJointActivity.class).putExtra("itemID",String.valueOf(ynShopCar.getCartItemID())));
            }
        });
        holder.name.setText(ynShopCar.getEbGoodsName());
        holder.enname.setText(ynShopCar.getEbGoodsEnName());
        final String EbGoodsPrice = ynShopCar.getEbGoodsPrice();
        final TextView tprice = holder.price;
        holder.num.setText(String.valueOf(ynShopCar.getCartItemNum()));
        final TextView tnum = holder.num;
        final ImageView ivsec = holder.sec;
        if (ynShopCar.getSelect()) {
            holder.sec.setImageResource(R.drawable.shopcar_select);
        } else {
            holder.sec.setImageResource(R.drawable.shopcar_unselect);
        }
        String zero=BigDecimalUtil.mul(tnum.getText().toString(), EbGoodsPrice);
        holder.price.setText("¥" + zero);
        tprice.setTag(zero);
        ynShopCar.setNowPrice(zero);
        holder.cut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemNum = Integer.parseInt(tnum.getText().toString());
                if (itemNum > 1) {
                    itemNum--;
                    tnum.setText(String.valueOf(itemNum));
                    String one = BigDecimalUtil.mul(String.valueOf(itemNum), EbGoodsPrice);
                    tprice.setText("¥" + one);
                    tprice.setTag(one);
                    if (ynShopCar.getSelect()) {
                        ShopCarListActivity.ynShopCarClearAll.remove(ynShopCar);
                        ynShopCar.setNowPrice(tprice.getTag().toString());
                        ynShopCar.setCartItemNum(Integer.parseInt(tnum.getText().toString()));
                        ShopCarListActivity.ynShopCarClearAll.add(ynShopCar);
                        context.sendBroadcast(new Intent(ACTIONs.actionUpdateHeJi));
                    }
                    else{
                        ynShopCar.setNowPrice(tprice.getTag().toString());
                        ynShopCar.setCartItemNum(Integer.parseInt(tnum.getText().toString()));
                    }
                } else {
                    Toast toast = Toast.makeText(context, "亲爱的，不能再少了哦~-(ㄒoㄒ)-~", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                }
            }
        });
        holder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemNum = Integer.parseInt(tnum.getText().toString());
                itemNum++;
                tnum.setText(String.valueOf(itemNum));
                String one = BigDecimalUtil.mul(String.valueOf(itemNum), EbGoodsPrice);
                tprice.setText("¥" + one);
                tprice.setTag(one);
                if (ynShopCar.getSelect()) {
                    ShopCarListActivity.ynShopCarClearAll.remove(ynShopCar);
                    ynShopCar.setNowPrice(tprice.getTag().toString());
                    ynShopCar.setCartItemNum(Integer.parseInt(tnum.getText().toString()));
                    ShopCarListActivity.ynShopCarClearAll.add(ynShopCar);
                    context.sendBroadcast(new Intent(ACTIONs.actionUpdateHeJi));
                }else{
                    ynShopCar.setNowPrice(tprice.getTag().toString());
                    ynShopCar.setCartItemNum(Integer.parseInt(tnum.getText().toString()));
                }
            }
        });
        holder.sec.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!ynShopCar.getSelect()) {
                    ynShopCar.setSelect(true);
                    ivsec.setImageResource(R.drawable.shopcar_select);
                    ynShopCar.setNowPrice(tprice.getTag().toString());
                    ynShopCar.setCartItemNum(Integer.parseInt(tnum.getText().toString()));
                    ShopCarListActivity.ynShopCarClearAll.add(ynShopCar);
                    context.sendBroadcast(new Intent(ACTIONs.actionUpdateHeJi));
                } else {
                    ynShopCar.setSelect(false);
                    ivsec.setImageResource(R.drawable.shopcar_unselect);
                    ShopCarListActivity.ynShopCarClearAll.remove(ynShopCar);
                    context.sendBroadcast(new Intent(ACTIONs.actionUpdateHeJi));
                }
                context.sendBroadcast(new Intent(ACTIONs.actionAllSelect3));
            }
        });
        if (position == listItems.size() - 1) {
            holder.shopcar_item_bline.setVisibility(View.GONE);
        } else {
            holder.shopcar_item_bline.setVisibility(View.VISIBLE);
        }
        return convertView;
    }


}